package com.xxxy.controller;

import com.xxxy.commonResult.CommonResult;
import com.xxxy.commonResult.CommonResultPage;
import com.xxxy.log.Log;
import com.xxxy.model.Course;
import com.xxxy.model.Student;
import com.xxxy.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author sjh
 * @date 2022/2/9 10:40
 */
@RestController
@Api(tags = "课程管理")
@RequestMapping("/course")
public class CourseController {

    @Autowired
    CourseService courseService;

    @Log(module = "课程管理", description = "获取课程列表")
    @GetMapping(value = "/list")
    @ApiOperation(value = "课程列表")
    public CommonResult<CommonResultPage> list(@ApiParam("页数") @RequestParam(defaultValue = "1") Integer page,
                                               @ApiParam("一页条数") @RequestParam(defaultValue = "10") Integer size,
                                               @ApiParam("学院id") @RequestParam(defaultValue = "0") Integer facultyId) {
        return CommonResult.success(courseService.list(page, size, facultyId));
    }

    @Log(module = "课程管理", description = "增加课程")
    @PostMapping(value = "/add")
    @ApiOperation(value = "增加课程")
    public CommonResult<Boolean> add(@ApiParam("课程信息") Course course) {
        boolean flag = courseService.save(course);
        if (flag) {
            CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Log(module = "课程管理", description = "删除课程")
    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除课程")
    public CommonResult<Boolean> delete(@ApiParam("课程id") Integer id) {
        boolean flag = courseService.removeById(id);
        if (flag) {
            CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Log(module = "课程管理", description = "修改课程")
    @PostMapping(value = "/update")
    @ApiOperation(value = "修改课程")
    public CommonResult<Boolean> update(@ApiParam("课程信息") Course course) {
        boolean flag = courseService.updateById(course);
        if (flag) {
            CommonResult.success();
        }
        return CommonResult.failed();
    }
}
